<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@page import="java.util.Date"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 <%@ page import = "java.sql.Connection" %>
        <%@ page import = "java.sql.DriverManager" %>
        <%@ page import = "java.sql.ResultSet" %>
        <%@ page import = "java.sql.Statement" %>
        <%@ page import = "java.sql.SQLException" %>
        <%@ page import = "java.sql.Connection" %>
<title>Registered</title>
</head>
<body>
<%
boolean isTrue = true;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/study", "root", "");
Statement st = connection.createStatement();
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String patronymic = request.getParameter("patronymic");
String academicdegree = request.getParameter("academicdegree");
String studyform = request.getParameter("studyform");
String faculty = request.getParameter("faculty");
String speciality = request.getParameter("speciality");
String entry_year = request.getParameter("entry_year");
String birthday = request.getParameter("birthday");
String iin = request.getParameter("iin");
String passport_number = request.getParameter("passport_number");
String idissuedby = request.getParameter("idissuedby");
String idvalidbefore = request.getParameter("idvalidbefore");
String sex = request.getParameter("sex");
String address = request.getParameter("address");
String tel = request.getParameter("tel");
String mail = request.getParameter("mail");
String photo = request.getParameter("file");
if(photo==null) photo = "";
String sql = "INSERT INTO student(firstname, lastname, patronymic, academicdegree_id, studyform_id, faculty_id, speciality_id, entry_year, birthday, iin, passport_number, idissuedby, idvalidbefore, sex, address, tel, mail, photo) "+
"VALUES('"+firstname+"' , '"+lastname+"' , '"
+patronymic+"', "+academicdegree+", "+studyform+", "+faculty+", "+speciality+
	", '"+entry_year+"', '"+birthday+"', '"+iin+"', '"+passport_number+"', '"+idissuedby+"',"
	+" '"+idvalidbefore+"', "+sex+", '"+address+"', '"+tel+"', '"+mail+"',"
+"'"+photo+"')";
st.executeUpdate(sql);
firstname = request.getParameter("mother_firstname");
lastname = request.getParameter("mother_lastname");
patronymic = request.getParameter("mother_patronymic");
address = request.getParameter("mother_address");
tel = request.getParameter("mother_tel");

sql = "INSERT INTO parent(firstname, lastname, patronymic, sex, address, tel) "+
"VALUES('"+firstname+"' , '"+lastname+"' , '"
+patronymic+"',2 , '"+address+"', '"+tel+"')";
st.executeUpdate(sql);

firstname = request.getParameter("father_firstname");
lastname = request.getParameter("father_lastname");
patronymic = request.getParameter("father_patronymic");
address = request.getParameter("father_address");
tel = request.getParameter("father_tel");

sql = "INSERT INTO parent(firstname, lastname, patronymic, sex, address, tel) "+
"VALUES('"+firstname+"' , '"+lastname+"' , '"
+patronymic+"',1 , '"+address+"', '"+tel+"')";
st.executeUpdate(sql);

sql = "SELECT * FROM student ORDER BY student_id ASC";
ResultSet rs = st.executeQuery(sql);
int student_id = 0;
while(rs.next()){
		student_id = rs.getInt("student_id");
}

String previousuniversity = request.getParameter("previousuniversity");
String yearcompleted = request.getParameter("yearcompleted");

sql = "INSERT INTO previous_education(student_id, studyplace, yearcompleted) "+
"VALUES("+student_id+", '"+previousuniversity+"' , '"+yearcompleted+"')";
st.executeUpdate(sql);

%>

<%
	

}
catch(Exception e){
    isTrue=false;
    %>
    <h1>Failed : <%=e.getMessage()%></h1>        
    <% 
    }
if(isTrue){%>
<h1>Sucessfully added!</h1>
<% } %>
<a href ="registration.jsp">to the Registration page</a>

 </c:if>
</body>
</html>